package cn.iocoder.yudao.module.artifact.service.info;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.artifact.controller.admin.info.vo.*;
import cn.iocoder.yudao.module.artifact.dal.dataobject.info.InfoDO;
import cn.iocoder.yudao.module.artifact.dal.dataobject.info.InfoImageDO;
import cn.iocoder.yudao.module.artifact.dal.dataobject.info.InfoVideoDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.artifact.dal.mysql.info.InfoMapper;
import cn.iocoder.yudao.module.artifact.dal.mysql.info.InfoImageMapper;
import cn.iocoder.yudao.module.artifact.dal.mysql.info.InfoVideoMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.artifact.enums.ErrorCodeConstants.*;

/**
 * 文玩信息 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class InfoServiceImpl implements InfoService {

    @Resource
    private InfoMapper infoMapper;
    @Resource
    private InfoImageMapper infoImageMapper;
    @Resource
    private InfoVideoMapper infoVideoMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createInfo(InfoSaveReqVO createReqVO) {
        // 插入
        InfoDO info = BeanUtils.toBean(createReqVO, InfoDO.class);
        infoMapper.insert(info);

        // 插入子表
        createInfoImageList(info.getId(), createReqVO.getInfoImages());
        createInfoVideoList(info.getId(), createReqVO.getInfoVideos());
        // 返回
        return info.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateInfo(InfoSaveReqVO updateReqVO) {
        // 校验存在
        validateInfoExists(updateReqVO.getId());
        // 更新
        InfoDO updateObj = BeanUtils.toBean(updateReqVO, InfoDO.class);
        infoMapper.updateById(updateObj);

        // 更新子表
        updateInfoImageList(updateReqVO.getId(), updateReqVO.getInfoImages());
        updateInfoVideoList(updateReqVO.getId(), updateReqVO.getInfoVideos());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteInfo(Long id) {
        // 校验存在
        validateInfoExists(id);
        // 删除
        infoMapper.deleteById(id);

        // 删除子表
        deleteInfoImageByArtifactId(id);
        deleteInfoVideoByArtifactId(id);
    }

    private void validateInfoExists(Long id) {
        if (infoMapper.selectById(id) == null) {
            throw exception(INFO_NOT_EXISTS);
        }
    }

    @Override
    public InfoDO getInfo(Long id) {
        return infoMapper.selectById(id);
    }

    @Override
    public PageResult<InfoDO> getInfoPage(InfoPageReqVO pageReqVO) {
        return infoMapper.selectPage(pageReqVO);
    }

    // ==================== 子表（文玩图片） ====================

    @Override
    public List<InfoImageDO> getInfoImageListByArtifactId(Long artifactId) {
        return infoImageMapper.selectListByArtifactId(artifactId);
    }

    private void createInfoImageList(Long artifactId, List<InfoImageDO> list) {
        if (list != null) {
            list.forEach(o -> o.setArtifactId(artifactId));
        }
        infoImageMapper.insertBatch(list);
    }

    private void updateInfoImageList(Long artifactId, List<InfoImageDO> list) {
        deleteInfoImageByArtifactId(artifactId);
		list.forEach(o -> o.setId(null).setUpdater(null).setUpdateTime(null)); // 解决更新情况下：1）id 冲突；2）updateTime 不更新
        createInfoImageList(artifactId, list);
    }

    private void deleteInfoImageByArtifactId(Long artifactId) {
        infoImageMapper.deleteByArtifactId(artifactId);
    }

    // ==================== 子表（文玩视频） ====================

    @Override
    public List<InfoVideoDO> getInfoVideoListByArtifactId(Long artifactId) {
        return infoVideoMapper.selectListByArtifactId(artifactId);
    }

    private void createInfoVideoList(Long artifactId, List<InfoVideoDO> list) {
        if (list != null) {
            list.forEach(o -> o.setArtifactId(artifactId));
        }
        infoVideoMapper.insertBatch(list);
    }

    private void updateInfoVideoList(Long artifactId, List<InfoVideoDO> list) {
        deleteInfoVideoByArtifactId(artifactId);
		list.forEach(o -> o.setId(null).setUpdater(null).setUpdateTime(null)); // 解决更新情况下：1）id 冲突；2）updateTime 不更新
        createInfoVideoList(artifactId, list);
    }

    private void deleteInfoVideoByArtifactId(Long artifactId) {
        infoVideoMapper.deleteByArtifactId(artifactId);
    }

}